import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meiyunapp/widget/goods_widget.dart';
import 'package:meiyunapp/widget/slide_image_page.dart';
import 'package:meiyunapp/widget/store_info_widget.dart';

import 'good_detail_controller.dart';

class GoodDetailPage extends StatelessWidget {
  late GoodDetailController _controller;

  GoodDetailPage({Key? key}) : super(key: key) {
    int id = Get.arguments;

    _controller = Get.put(GoodDetailController(), tag: "$id");
    _controller.getData("$id");
  }



  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: _controller.obx(
        (data) {
          return Scaffold(
            appBar: AppBar(
              title: Text(data!.name),
            ),
            body: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SlideImagePageWidget(data.images),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          data.name,
                          style: const TextStyle(fontSize: 21),
                        ),
                        Container(
                          decoration: const BoxDecoration(
                              color: Colors.red,
                              borderRadius: BorderRadius.all(Radius.circular(2))),
                          child: Text(
                            "券后¥${data.activePrice}",
                            style: const TextStyle(
                                fontSize: 12, color: Colors.white),
                          ),
                        ),
                        const SizedBox(
                          height: 10,
                        ),
                        RichText(
                          text: TextSpan(
                            children: [
                              const TextSpan(
                                  text: "体验价:",
                                  style:
                                      TextStyle(fontSize: 14, color: Colors.red)),
                              TextSpan(
                                  text:
                                      "\t\t¥${data.activePrice.toStringAsFixed(2)}\t\t",
                                  style: const TextStyle(
                                      fontSize: 18, color: Colors.red)),
                              TextSpan(
                                  text: "¥${data.costPrice.toStringAsFixed(2)}",
                                  style: const TextStyle(
                                    fontSize: 12,
                                    color: Color(0xff666666),
                                    decoration: TextDecoration.lineThrough,
                                  ))
                            ],
                          ),
                        ),
                        StoreInfoWidget(data.store),
                      ],
                    ),
                  ),
                  Container(
                    width: double.infinity,
                    height: 40,
                    color: Colors.black26,
                    alignment: Alignment.centerLeft,
                    padding: const EdgeInsets.only(left: 10),
                    child: const Text(
                      "购买须知",
                      style: TextStyle(fontSize: 16),
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.all(10),
                    child: Wrap(
                      runSpacing: 5.0,
                      children: [
                        SizedBox(
                          width: MediaQuery.of(context).size.width / 2 - 10,
                          child: RichText(
                            text: TextSpan(
                              children: [
                                const TextSpan(
                                    text: "核销码有效期\n",
                                    style: TextStyle(color: Colors.black)),
                                TextSpan(
                                    text: data.availableDays,
                                    style: const TextStyle(
                                        color: Colors.black, height: 1.5)),
                              ],
                            ),
                          ),
                        ),
                        SizedBox(
                          width: MediaQuery.of(context).size.width / 2 - 10,
                          child: RichText(
                            text: TextSpan(
                              children: [
                                const TextSpan(
                                    text: "适用人群\n",
                                    style: TextStyle(color: Colors.black)),
                                TextSpan(
                                    text: data.genderText,
                                    style: const TextStyle(
                                        color: Colors.black, height: 1.5)),
                              ],
                            ),
                          ),
                        ),
                        const SizedBox(
                          height: 10,
                          width: double.infinity,
                        ),
                        SizedBox(
                          width: MediaQuery.of(context).size.width / 2 - 10,
                          child: RichText(
                            text: TextSpan(
                              children: [
                                const TextSpan(
                                    text: "预约信息\n",
                                    style: TextStyle(color: Colors.black)),
                                TextSpan(
                                    text: data.reservation,
                                    style: const TextStyle(
                                        color: Colors.black, height: 1.5)),
                              ],
                            ),
                          ),
                        ),
                        SizedBox(
                          width: MediaQuery.of(context).size.width / 2 - 10,
                          child: RichText(
                            text: TextSpan(
                              children: [
                                const TextSpan(
                                    text: "项目时长\n",
                                    style: TextStyle(color: Colors.black)),
                                TextSpan(
                                    text: "${data.duration ?? "?"}分钟",
                                    style: const TextStyle(
                                        color: Colors.black, height: 1.5)),
                              ],
                            ),
                          ),
                        ),
                        const SizedBox(
                          height: 15,
                          width: double.infinity,
                        ),
                        SizedBox(
                          width: MediaQuery.of(context).size.width - 10,
                          child: RichText(
                            text: TextSpan(
                              children: [
                                const TextSpan(
                                    text: "退款信息\n",
                                    style: TextStyle(color: Colors.black)),
                                TextSpan(
                                    text: data.notice,
                                    style: const TextStyle(
                                        color: Colors.black, height: 1.6)),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Visibility(
                    visible: data.store.products.isNotEmpty,
                    child: Container(
                      width: double.infinity,
                      height: 40,
                      color: Colors.black26,
                      alignment: Alignment.centerLeft,
                      padding: const EdgeInsets.only(left: 10),
                      child: const Text(
                        "相关产品",
                        style: TextStyle(fontSize: 16),
                      ),
                    ),
                  ),
                  Column(
                      children: data.store.products
                          .map((e) => GoodsWidget(e))
                          .toList()),
                  Container(
                    width: double.infinity,
                    height: 40,
                    color: Colors.black26,
                    alignment: Alignment.centerLeft,
                    padding: const EdgeInsets.only(left: 10),
                    child: const Text(
                      "商品详情",
                      style: TextStyle(fontSize: 16),
                    ),
                  ),
                  CachedNetworkImage(imageUrl: data.descImages[0])
                ],
              ),
            ),
          );
        },
        onLoading: const CupertinoActivityIndicator(),
      ),
    );
  }
}
